#!/usr/bin/python
#-*-coding:utf-8-*-       #指定编码格式，python默认unicode编码

# 要求使用python 3
# 依赖pypinyin包
# 安装方法:
#     终端下运行pip3 install pypinyin

from pypinyin import lazy_pinyin,Style
import json,os,sys,shutil
import re

source_folder ="./res/"

def contains_chinese(filename):
    return bool(re.search('[\u4e00-\u9fff]', filename))

def action():
    file_list=os.listdir(source_folder)
    for file_obj in file_list:
        file_path=os.path.join(source_folder,file_obj)

        file_name,file_extend=os.path.splitext(file_obj)

        remove_path=os.path.join(source_folder,file_obj)
        # print(file_name)

        new_name=file_name
        # print(new_name)

        result = contains_chinese(new_name)
        # print(result)
        if result == False:
            continue

        my_new_name = []
        # print(new_name)
        # print(type(new_name))

        name = lazy_pinyin(new_name)
        result = ''
        for i in name:
            result +=i
        # print(result)
        # break

        result += file_extend
        # print(result)

        newfile_path=os.path.join(source_folder,result)

        # print(newfile_path)

        shutil.copyfile(file_path,newfile_path)

        # 删除中文名文件
        # print(remove_path)
        os.remove(remove_path)



def run():
    action()

    # s= lazy_pinyin('中心')

    # print(s)

run()
